<template>
  <basic-container>
    <avue-crud
      ref="scopeTree"
      :data="tableData"
      :option="option"
      :page="page"
      :destroy-on-close="true"
      @on-load="onLoad"
      @refresh-change="searchHandler"
      @row-del="rowDelete"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
    >
    </avue-crud>
  </basic-container>
</template>

<script>
import { getTerm, addTerm, updateTerm, deleteTerm } from "@/api/basic/term";

export default {
  name: "term",
  data() {
    return {
      page: {
        pageSize: 10,
        total: 10
      },
      searchForm: {
        currentPage: 1,
        pageSizes: 10
      },
      tableData: [],
      option: {
        border: true,
        index: true,
        indexLabel: "序号",
        stripe: true,
        menuAlign: "center",
        editBtn: true,
        delBtn: true,
        searchMenuSpan: 6,
        align: "center",
        addBtn: true,
        viewBtn: true,
        column: [
          {
            search: true,
            label: "学年",
            prop: "year",
            span: 24,
            rules: [
              {
                required: true,
                message: "学年不能为空",
                trigger: "blur"
              }
            ]
          },
          {
            search: true,
            label: "学期",
            display: false,
            prop: "term",
            span: 24,
            rules: [
              {
                required: true,
                message: "学期不能为空",
                trigger: "blur"
              }
            ]
          },
          {
            search: true,
            label: "是否默认学期",
            prop: "isCurrent",
            span: 24,
            addDisplay: false,
            type: "select",
            dicData: [
              {
                label: "是",
                value: true
              },
              {
                label: "否",
                value: false
              }
            ],
            rules: [
              {
                required: true,
                message: "学期不能为空",
                trigger: "blur"
              }
            ]
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            editDisplay: false,
            addDisplay: false
          }
        ]
      }
    };
  },
  methods: {
    rowSave(form, done, loading) {
      loading();
      addTerm(form)
        .then(() => {
          this.$message.success("添加成功");
          this.searchHandler();
        })
        .finally(() => {
          done();
        });
    },
    rowUpdate(row, index, done, loading) {
      loading();
      updateTerm(row)
        .then(() => {
          this.$message.success("更新成功");
          this.searchHandler();
        })
        .finally(() => {
          done();
        });
    },
    rowDelete(form) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteTerm([form.id]).then(() => {
            this.$message.success("删除成功");
            this.searchHandler();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    searchChange(params, done) {
      this.searchForm = params;
      this.searchForm.currentPage = 1;
      this.search9Form.pageSizes = 10;
      this.searchHandler();
      done();
    },
    onLoad(page) {
      this.searchForm.currentPage = page.currentPage;
      this.searchForm.pageSize = page.pageSize;
      this.searchHandler();
    },
    searchHandler() {
      console.log("11111111111111111111");
      getTerm(this.searchForm).then(res => {
        this.tableData = res.data.records;
        this.page.total = res.data.total;
      });
    }
  }
};
</script>

<style scoped></style>
